
#include <iostream>

#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/highgui/highgui.hpp>

#include <vector>

using namespace cv;
using namespace std;

int main(int argc,char* argv[])
{
	bool g_step=false ;
	if(argc<3){
		cout<<"usage ./fast -i vieofile [--step]"<<endl;
		return -1 ;
	}
	VideoCapture capture(argv[2]);
	if(argc==4){
		string step(argv[3]);
		if(step=="--step"){
			g_step = true ;
		}
	}
	if(!capture.isOpened()){
		cout<<"fail to open "<<argv[2]<<endl;
		return -1 ;
	}
	Mat srcImage;

	int frame_cnt=0;
	while(1)
	{

		if(!capture.read(srcImage))
		{
			cout<<"读取视频失败"<<endl;
			break;
		}

		cv::cvtColor(srcImage, srcImage, CV_BGR2GRAY);
		cv::resize(srcImage, srcImage, cv::Size(800, 600), (0, 0), (0, 0), cv::INTER_LINEAR);
		cout<<"读取帧："<<frame_cnt<<endl;
		imshow("origin_frame",srcImage);
//		Mat srcImage = imread(argv[1]);
//		cv::resize(srcImage, srcImage, cv::Size(800, 600), (0, 0), (0, 0), cv::INTER_LINEAR);

		Mat srcGrayImage;
		if (srcImage.channels() == 3)
		{
		//		cv::cvtColor(srcImage,srcGrayImage,CV_RGB2GRAY);
			cv::cvtColor(srcImage, srcGrayImage, CV_BGR2GRAY);
		}
		else
		{
			srcImage.copyTo(srcGrayImage);
		}
		vector<KeyPoint>detectKeyPoint;
		Mat keyPointImage1,keyPointImage2;

		Ptr<FastFeatureDetector> fast = FastFeatureDetector::create();
		fast->detect(srcGrayImage,detectKeyPoint);
		drawKeypoints(srcImage,detectKeyPoint,keyPointImage1,Scalar(0,0,255),DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
		//drawKeypoints(srcImage,detectKeyPoint,keyPointImage2,Scalar(0,0,255),DrawMatchesFlags::DEFAULT);

		imshow("keyPoint image1",keyPointImage1);
//		imshow("keyPoint image2",keyPointImage2);

        frame_cnt++;
        if(g_step){
       	 waitKey(0);
        }else{
            waitKey(1);
        }
	}
	return 0;
}

